﻿// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System.Data;

namespace SnapObjects.Data.Odbc
{
    internal class OdbcSqlAnywhereSqlColumnSchemaProvider : SqlColumnSchemaProvider
    {
        public OdbcSqlAnywhereSqlColumnSchemaProvider(DataContext dataContext) : base(dataContext)
        {
        }

        protected override bool IsExtraHandleNullable => true;

        protected override bool IsExpression(DataRow row, IDataReader reader)
        {
            var column = row.Table.Columns["isreadonly"];

            var value = row.ItemArray[column.Ordinal];

            return this.GetSchemaItemValue<bool>(value);
        }

        protected override bool IsReadOnly(DataRow row, IDataReader reader)
        {
            return this.IsExpression(row, reader);
        }

        protected override bool IsIdentity(DataRow row, IDataReader reader)
        {
            var column = row.Table.Columns["isautoincrement"];

            var value = row.ItemArray[column.Ordinal];

            return this.GetSchemaItemValue<bool>(value);
        }

        protected override string ColumnDataTypeName(DataRow row, IDataReader reader)
        {
            var ordinal = this.ColumnOrdinal(row, reader);

            if (reader != null)
            {
                return reader.GetDataTypeName(ordinal);
            }

            return null;
        }

        protected override bool IsHidden(DataRow row, IDataReader reader)
        {
            return false;
        }
    }
}
